import numpy as np
import tensorflow as tf
import joblib
import sys

if len(sys.argv) != 4:
    print("Error: Please provide 3 input parameters.")
    sys.exit(1)

# 从命令行参数获取输入数据
input_file_path = sys.argv[1] # 替换为你的输入文件路径
output_file_path = sys.argv[2]
limit = float(sys.argv[3])



# 使用绝对路径加载训练好的LSTM模型和标准化器
model_path = 'E:/TakeOut/Geers/sky-common/src/main/resources/result/lstm_model.h5'  # 替换为你的模型路径
scaler_path = 'E:/TakeOut/Geers/sky-common/src/main/resources/result/scaler.pkl'  # 替换为你的标准化器路径

# 加载训练好的LSTM模型
model = tf.keras.models.load_model(model_path)

# 加载已保存的标准化器
scaler = joblib.load(scaler_path)

# 读取输入数据文件
input_data = np.loadtxt(input_file_path, delimiter=',',skiprows=1)
# 使用加载的标准化器进行数据标准化
input_data_normalized = scaler.transform(input_data)  # 将所有输入数据标准化

# 调整输入形状以符合LSTM的输入要求
input_data_reshaped = input_data_normalized.reshape((input_data_normalized.shape[0], 1, input_data_normalized.shape[1]))

# 进行预测
predictions = model.predict(input_data_reshaped)



# 计算所有预测结果的和
predictions = (predictions > limit ).astype("int32")

# 使用 np.concatenate 列合并这两个数组
result = np.concatenate((input_data, predictions), axis=1)

np.savetxt(output_file_path, result, fmt='%s', delimiter=',',header="attr_one,attr_two,attr_three,attr_four,load,result")


sum_of_predictions = sum(predictions)
with open(output_file_path, 'a') as file:
    file.write(f"regard geers that are bigger than {limit}  as normal geers , sum = {sum_of_predictions}")


